<template>
  <div class='container'>
    <el-dialog title="新增技巧" :visible="showDialog" width="60%" @close="close">
      <el-form ref="form" :model="form" label-width="100px" :rules="formRules">
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody">
          <quill-editor v-model="form.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址：">
          <el-input v-model="form.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('control', false)">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 表单数据
      form: {
        title: '', // 标题
        articleBody: '', // 文章正文
        videoURL: ''// 视频地址
      },
      // 表单验证规则
      formRules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close () {
      // 向父组件传递控制子组件关闭的值
      this.$emit('control', false)
      // 清空表单
      this.form = {
        title: '', // 标题
        articleBody: '', // 文章正文
        videoURL: ''// 视频地址
      }
      this.$refs.form.resetFields()
      this.$emit('Refresh')
    },
    // 确定提交表单函数
    async determine () {
      console.log(this.number)
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          // 修改
          await update(this.form)
        } else {
          // 新增
          await add(this.form)
        }
        this.$emit('Refresh')
        this.$emit('control', false)
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 接收行数据的函数
    hangData (data) {
      this.form = data
    }
  }
}
</script>

<style scoped lang='scss'>
// 设置富文本编辑器的默认最小高度
::v-deep .ql-editor {
  min-height: 150px;
}

// 解决元素对齐问题
::v-deep .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

// 解决元素对齐问题
::v-deep .ql-picker-label > svg {
  position: relative;
  top: -6px;
}
</style>
